<template>
    <div>
       <el-form :model="Form" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
  <el-form-item label="用户名称" prop="name">
    <el-input v-model="Form.name" size="mini" style="width:300px"></el-input>
  </el-form-item>
  <el-form-item label="密码" prop="password">
    <el-input type="password" v-model="Form.password"></el-input>
  </el-form-item>
  <el-form-item label="电话" prop="phone">
    <el-input type="phone" v-model="Form.phone"></el-input>
  </el-form-item>
  <el-form-item label="角色" prop="role">
    <el-select v-model="Form.role" placeholder="请选择角色">
      <el-option label="管理员" value="admin"></el-option>
      <el-option label="普通用户" value="user"></el-option>
    </el-select>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
    <el-button @click="resetForm('ruleForm')">重置</el-button>
  </el-form-item>
</el-form>
    </div>
  </template>
  <script>
  import { addUser } from '../../api';
  // 导入组件
 export default {
    name: 'addGoods',
    data() {
      return {
        Form: {
          name: '',
          password: '',
          role: '',
          phone: '',
           },
        rules: {
            name: [
            { required: true, message: '请输入产品名称', trigger: 'blur' },
            { min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur',
            min: 6, max: 8, message: '长度在 6 到 8 个字符', }
          ],
          role: [
            { required: true, message: '请选择角色', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '请输入电话', trigger: 'blur',
            min: 11, max: 11, message: '请输入正确电话号码', trigger: 'blur' }
          ]}
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let data={
              nick:this.Form.name,
             password:this.Form.password,
             phone:this.Form.phone,
             role:this.Form.role
            }
            addUser(data).then(res=>{
              
              if(res.code ==200){
                this.$message.success('添加成功')
              }else{
                this.$message.error('添加失败')
              }
            })
          } else {
           this.$message.error("请正确输入表单信息!")
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
  </script>
  <style >

  </style>
  